package com.kfm.springmybatis.Collection;

import com.kfm.springmybatis.model.User;
import com.kfm.springmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginCollection {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public String loginUSer(String username, String password, HttpServletRequest request,
                            HttpServletResponse response, String captcha, RedirectAttributes attributes) {
        List<User> user = userService.getAllUserByUsernameAndPassword(username, password);
        Integer allUserByRoleAndName = userService.getAllUserByRoleAndName(username, password);
        if (allUserByRoleAndName == 1) {
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            return "redirect:/index";
        } else {
            attributes.addFlashAttribute("message", "用户名或密码错误");
            return "redirect:/login";
        }
    }

//    public Integer getUserId(String username, String password) {
//
//    }
}
